<?php
/**
 * @file indigo/framework/classes/ModuleFactory.php
 * Contains the definition for the ModuleFactor class
 */
  namespace indigo\framework;
  
  /**
   * @class ModuleFactory
   * Factory class for loading, and initializing modules
   */
	class ModuleFactory
	{
   /**
    * Loads a module of a given $moduleName if it exists
    *
    * Instantiates an object that is a subclass of the Module class, loads its settings file, and runs the Module::initialize() method
    *
    * @param $moduleName the fully qualified class name of the module. It has to be a subclass of the Module class
    *
    * @returns The instance of the module on success, or one of the return values below:
    * @retval -1 if the settings file could not be loaded
    * @retval -2 if the module could not be initialize
    * @retval false if the module provided is not a subclass of Module
    */
		public static function load($moduleName)
		{
			if (is_subclass_of($moduleName, 'indigo\framework\Module')) {
				$module = new $moduleName();

				if (!$module->loadSettings()) {
					return -1;
        }
        
				if (($return = $module->initialize()) !== FALSE) {
					return $module;
        } else {
          return -2;
        }
			}	
      
      return false;
		}
	}